package ch02.home;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.management.Attribute;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;

public class Home02 {
    public static void add(Document document){
       try {
           Element student = document.createElement("student");
           student.setAttribute("id","3");
           Element name = document.createElement("name");
           name.setTextContent("树树");
           student.appendChild(name);
           Element course = document.createElement("course");
           course.setTextContent("语文");
           student.appendChild(course);
           Element score = document.createElement("score");
           score.setTextContent("99");
           student.appendChild(score);
           Node scores = document.getElementsByTagName("scores").item(0);
           scores.appendChild(student);
       }catch (Exception e){
           e.printStackTrace();
       }
    }

    public static void update(Document document){
        try {
            NodeList student = document.getElementsByTagName("student");
            for (int i=0;i<student.getLength();i++){
                 Element element = (Element)student.item(i);
                 if (element.getAttribute("id").equals("2")){
                     Node score = element.getElementsByTagName("score").item(0);
                    score.setTextContent("60");
                 }
            }
        }catch (Exception e){

        }
    }

    public static void delete(Document document){
        try {
            NodeList student = document.getElementsByTagName("student");
            for (int i=0;i<student.getLength();i++){
                Element element=(Element)student.item(i);
                if (element.getAttribute("id").equals("1")){
                    Node score = document.getElementsByTagName("score").item(0);
                    element.removeChild(score);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        try {
            DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
            DocumentBuilder db=dbf.newDocumentBuilder();
            Document document=db.parse("src/ch02/school.xml");
            NodeList node=document.getElementsByTagName("student");
            for (int i=0;i<node.getLength();i++){
                Element item = (Element) node.item(i);
                String id = item.getAttribute("id");
                Node name = item.getElementsByTagName("name").item(0);
                String nameTextContent = name.getTextContent();
                Node course = item.getElementsByTagName("course").item(0);
                String courseTextContent = course.getTextContent();
                Node score = item.getElementsByTagName("score").item(0);
                String scoreTextContent = score.getTextContent();
                System.out.print(id);
                System.out.print("\t"+nameTextContent);
                System.out.print("\t"+courseTextContent);
                System.out.print("\t"+scoreTextContent);
                System.out.println();
            }
//            add(document);
//            update(document);
            delete(document);
            TransformerFactory transformerFactory=TransformerFactory.newInstance();
            Transformer transformer=transformerFactory.newTransformer();
            DOMSource domSource=new DOMSource(document);
            StreamResult result=new StreamResult(new File("src/ch02/school1.xml"));
            transformer.transform(domSource,result);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
